<?php
/**
 * Created by PhpStorm.
 * User: lhl
 * Date: 2020/10/12
 * Time: 10:06
 * Features:后台基础控制器
 */
namespace app\adminapiv3\controller;

use app\common\exception\CustomException;
use app\common\service\Host;
use app\common\traits\BaseServiceTrait;
use think\Controller;
use think\facade\Request;

class BaseController extends Controller
{
    use BaseServiceTrait;

    //验证器验证失败后自动抛出异常,配合异常接管类自动返回json格式数据,不要修改基类的此属性！！
    protected $failException = true;

    /**
     * 控制器初始化
    */
    protected function initialize()
    {
        global $errCode;
        $errCode = $this->CODE_SUCCESS;
        global $errMsg;
        $errMsg = '操作已成功!';
        //下发命令至客户端结果
        global $directiveResult;
        $directiveResult = null;
        $this->setHostId();
    }

    /**
     * @name: 过滤 信息输入
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-05-22 17:46:03
     */
    public function filterInput ($index, $method = 'post', $default = '')
    {

        $hav = false;
        if (strstr($index, '*'))
        {
            $index = substr($index,0, -1);
            $hav = true;
        }

        if (empty($index) || $index == 'data')
        {
            $inputData = $this->clearEmoji(Request::$method($index,'',null));
        }
        else
        {
            $inputData = $this->clearEmoji(Request::$method($index, $default,null));
        }

        if ($hav && (empty($inputData) && !is_numeric($inputData)))
        {
            header('Content-Type:application/json;charset=utf-8');
            echo json_encode([
                'code' => 201,
                'msg' => '[' . strtoupper($index) . ']' . '为必须参数',
            ],JSON_UNESCAPED_UNICODE);
            die;
        }

        if(is_array($inputData)){
            return $inputData;
        }


        return trim($inputData);
    }

    /**
     * @name: 清除emoji
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-12-21 13:43:00
     */
    public function clearEmoji ($str)
    {
        return clear_emoji($str);
    }

    /**
     * @name: 设置主机id
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-15 13:54:01
     */
    private function setHostId ()
    {
        $host_id = Request::param('host_id');
        if(!$host_id){//没有host_id,跳过验证
            return true;
        }

        preg_match_all('/,?(\d+),?/i',$host_id, $host_id_a);
        if(empty($host_id_a))
        {
           throw new CustomException('错误的主机id', 422);
        }

        if(strlen($host_id) != strlen(implode(',', $host_id_a[1])))
        {
           throw new CustomException('错误的主机id', 422);
        }
        $Host = new Host();
        $online = $Host->hostList($host_id_a[1], 'id,name,os,ip,alias,address_id,hostkey');
        if (empty($online))
        {
           throw new CustomException('主机不存在或已被删除', 422);
        }
        $exsitHost = array_keys($online);

        //验证资产操作权限

        //获取当前用户资产组
        $hostGroupId = $this->getLoginHostGroupId();

        if(!$hostGroupId){
            throw new CustomException('资产组不存在', 422);
        }

        $groupHostArr = $Host->getGroupHost($hostGroupId);

        $notExsiHost = $notPermission = [];
        foreach ($host_id_a[1] as $v){
            if (!in_array($v, $exsitHost)) {
                $notExsiHost[] = $v;
            }
            if (!in_array($v, $groupHostArr)) {
                $notPermission[] = $v;
            }
        }

        if ($notExsiHost)
        {
           throw new CustomException('主机id为:' . implode('，', $notExsiHost) . '不存在或已被删除', 422);
        }

        if ($notPermission)
        {
            $notPermissionHostName = [];
            foreach ($notPermission as $value){
                $notPermissionHostName[] = $online[$value]['name'] ;
            }
           throw new CustomException('您当前对资产名称为:' . implode('，', $notPermissionHostName) . '没有操作权限', 422);
        }
        global $online_host;
        $online_host = $online;
    }


    /**
     * @name: 重新设置在线主机
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-13 18:24:26
     */
    public function resetOnlineHost ($host)
    {
        global $online_host;
        $online_host = $host;
    }

}